
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>模型类参考 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="模型 Meta 选项" href="options.html" />
    <link rel="prev" title="关联对象参考" href="relations.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="relations.html" title="关联对象参考">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="options.html" title="模型 &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;Meta&lt;/span&gt;&lt;/code&gt; 选项">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-models-class">
            
  <div class="section" id="s-model-class-reference">
<span id="model-class-reference"></span><h1>模型类参考<a class="headerlink" href="#model-class-reference" title="永久链接至标题">¶</a></h1>
<p>本文档涵盖了 <a class="reference internal" href="instances.html#django.db.models.Model" title="django.db.models.Model"><code class="xref py py-class docutils literal notranslate"><span class="pre">Model</span></code></a> 类的功能。关于模型的更多信息，请参见 <a class="reference internal" href="index.html"><span class="doc">模型参考指南的完整列表</span></a>。</p>
<div class="section" id="s-attributes">
<span id="attributes"></span><h2>属性<a class="headerlink" href="#attributes" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-doesnotexist">
<span id="doesnotexist"></span><h3><code class="docutils literal notranslate"><span class="pre">DoesNotExist</span></code><a class="headerlink" href="#doesnotexist" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.db.models.Model.DoesNotExist">
<em class="property">exception </em><code class="descclassname">Model.</code><code class="descname">DoesNotExist</code><a class="headerlink" href="#django.db.models.Model.DoesNotExist" title="永久链接至目标">¶</a></dt>
<dd><p>当没有找到预期的对象时，ORM 会引发这个异常。例如， <a class="reference internal" href="querysets.html#django.db.models.query.QuerySet.get" title="django.db.models.query.QuerySet.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet.get()</span></code></a> 将在没有找到给定查找对象时引发该异常。</p>
<p>Django 提供了一个 <code class="docutils literal notranslate"><span class="pre">DoesNotExist</span></code> 的异常作为每个模型类的属性，用来标识找不到的对象类，允许你为某个模型类捕捉异常。该异常是 <a class="reference internal" href="../exceptions.html#django.core.exceptions.ObjectDoesNotExist" title="django.core.exceptions.ObjectDoesNotExist"><code class="xref py py-exc docutils literal notranslate"><span class="pre">django.core.exceptions.ObjectDoesNotExist</span></code></a> 的一个子类。</p>
</dd></dl>

</div>
<div class="section" id="s-multipleobjectsreturned">
<span id="multipleobjectsreturned"></span><h3><code class="docutils literal notranslate"><span class="pre">MultipleObjectsReturned</span></code><a class="headerlink" href="#multipleobjectsreturned" title="永久链接至标题">¶</a></h3>
<dl class="exception">
<dt id="django.db.models.Model.MultipleObjectsReturned">
<em class="property">exception </em><code class="descclassname">Model.</code><code class="descname">MultipleObjectsReturned</code><a class="headerlink" href="#django.db.models.Model.MultipleObjectsReturned" title="永久链接至目标">¶</a></dt>
<dd><p>当给定的查找找到多个对象时， <a class="reference internal" href="querysets.html#django.db.models.query.QuerySet.get" title="django.db.models.query.QuerySet.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet.get()</span></code></a> 会引发这个异常。</p>
<p>Django 提供了一个 <code class="docutils literal notranslate"><span class="pre">MultipleObjectsReturned</span></code> 的异常作为每个模型类的属性，用于识别发现多个对象的对象类，允许你为某个模型类捕捉异常。该异常是 <a class="reference internal" href="../exceptions.html#django.core.exceptions.MultipleObjectsReturned" title="django.core.exceptions.MultipleObjectsReturned"><code class="xref py py-exc docutils literal notranslate"><span class="pre">django.core.exceptions.MultipleObjectsReturned</span></code></a> 的一个子类。</p>
</dd></dl>

</div>
<div class="section" id="s-objects">
<span id="objects"></span><h3><code class="docutils literal notranslate"><span class="pre">objects</span></code><a class="headerlink" href="#objects" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Model.objects">
<code class="descclassname">Model.</code><code class="descname">objects</code><a class="headerlink" href="#django.db.models.Model.objects" title="永久链接至目标">¶</a></dt>
<dd><p>每个非抽象的 <a class="reference internal" href="instances.html#django.db.models.Model" title="django.db.models.Model"><code class="xref py py-class docutils literal notranslate"><span class="pre">Model</span></code></a> 类必须有一个 <a class="reference internal" href="../../topics/db/managers.html#django.db.models.Manager" title="django.db.models.Manager"><code class="xref py py-class docutils literal notranslate"><span class="pre">Manager</span></code></a> 实例添加到其中。Django 确保在你的模型类中至少指定了一个默认的 <code class="docutils literal notranslate"><span class="pre">Manager</span></code>。如果你没有添加自己的 <cite>Manager`</cite>，Django 会添加一个属性 <code class="docutils literal notranslate"><span class="pre">objects</span></code>，包含默认的 <a class="reference internal" href="../../topics/db/managers.html#django.db.models.Manager" title="django.db.models.Manager"><code class="xref py py-class docutils literal notranslate"><span class="pre">Manager</span></code></a> 实例。如果添加自己的 <a class="reference internal" href="../../topics/db/managers.html#django.db.models.Manager" title="django.db.models.Manager"><code class="xref py py-class docutils literal notranslate"><span class="pre">Manager</span></code></a> 实例属性，则不会出现默认的。考虑下面的例子：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c1"># Add manager with another name</span>
    <span class="n">people</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">()</span>
</pre></div>
</div>
<p>关于模型管理器的更多细节，请参见 <a class="reference internal" href="../../topics/db/managers.html"><span class="doc">管理器</span></a> 和 <a class="reference internal" href="../../topics/db/queries.html#retrieving-objects"><span class="std std-ref">检索对象</span></a>。</p>
</dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">模型类参考</a><ul>
<li><a class="reference internal" href="#attributes">属性</a><ul>
<li><a class="reference internal" href="#doesnotexist"><code class="docutils literal notranslate"><span class="pre">DoesNotExist</span></code></a></li>
<li><a class="reference internal" href="#multipleobjectsreturned"><code class="docutils literal notranslate"><span class="pre">MultipleObjectsReturned</span></code></a></li>
<li><a class="reference internal" href="#objects"><code class="docutils literal notranslate"><span class="pre">objects</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="relations.html"
                        title="上一章">关联对象参考</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="options.html"
                        title="下一章">模型 <code class="docutils literal notranslate"><span class="pre">Meta</span></code> 选项</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/models/class.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="relations.html" title="关联对象参考">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="options.html" title="模型 &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;Meta&lt;/span&gt;&lt;/code&gt; 选项">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>